package com.audi.services;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import android.annotation.TargetApi;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.SystemClock;
import android.text.format.Time;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;

import com.audi.R;
import com.audi.activity.SmartTriggerActivity;

public class TestService extends Service{
	AlarmManager am;
	PendingIntent  pi;
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		Log.e("=====SERVICE=========", "service is starting");
		new Thread(new task()).start();
		//setup();
		testTimerShedule();
		return super.onStartCommand(intent, flags, startId);
	}
	
	
	
	@TargetApi(Build.VERSION_CODES.JELLY_BEAN) @SuppressWarnings("deprecation")
	public void pushNotification(){
		Intent intent = new Intent(this, SmartTriggerActivity.class);
		PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

		// build notification
		// the addAction re-use the same intent to keep the example short
		Notification n  = new Notification.Builder(this)
		        .setContentTitle("%alarm_sensor_name% has triggered!")
		        .setContentText("%alarm_sensor_name% has triggered!")
		        .setSmallIcon(R.drawable.icon_plug_on_off)
		        .setContentIntent(pIntent)
		        .setAutoCancel(true)
		        .addAction(R.drawable.icon_plug_on_off, "Stop alarm bell", pIntent)
		        .build();
		    
		  
		NotificationManager notificationManager = 
		  (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

		notificationManager.notify(0, n); 
	}
	class task implements Runnable{
		@Override
		public void run() {

			for (int i = 0; i < 10; i++) {
				try {
					Thread.sleep(1000);
					Log.d("==================", ">>>>>> "+i+" ======");
//					am.set( AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 
//							5000, pi );
					
						//pushNotification();
					
					
				} catch (Exception e) {
					// TODO: handle exception
					Log.e("==================", e.toString()+"...======");
				}
				
			}
		} 
	}
	private void setup() {

		BroadcastReceiver br = new BroadcastReceiver() {
			
			@Override
			public void onReceive(Context context, Intent intent) {
				// test dialog with service
				Toast.makeText(context, "alarm", Toast.LENGTH_SHORT).show();
				Log.e("===========Alarm", "ALarm==============");
				AlertDialog alertDialog = new AlertDialog.Builder(getApplicationContext()).create();
				alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
				alertDialog.setTitle("Warning detected");
				alertDialog.setMessage("...................");
				alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
				// here you can add functions
				}
				});
				alertDialog.setIcon(R.drawable.action_add);
				alertDialog.show();
				
				
			}
		};
		registerReceiver(br, new IntentFilter("com.authorwjf.wakeywakey"));
		pi = PendingIntent.getBroadcast( this, 0, new Intent("com.authorwjf.wakeywakey"),
				  0 );
		
		
		am=(AlarmManager)
				(this.getSystemService( Context.ALARM_SERVICE ));
	}
	public void testTimerShedule(){
		final Handler handler=new Handler();
		Timer timer=new Timer(false);
		
		TimerTask timerTask=new TimerTask() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				//Log.d("===== task schedule", "=========== ");
				handler.post(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						Calendar c = Calendar.getInstance(); 
						Log.d("===== task schedule", "=========== refeshs== "+ c.get(Calendar.SECOND));
					}
				});
			}
		};
		timer.schedule(timerTask, 3*1000,3000);
		
	}
}
